import axios from "axios";


// let host = "http://njxs.weilian.cn";
      let host = "http://test.njxs.weilian.cn";
// let host = "http://172.19.5.131:33004";
// let host = "http://172.19.5.145:8112";
// let host = "http://172.19.5.193:8112";
// let host = 'http://172.19.6.150'
// host = 'http://172.19.5.89:33003'
let port = window.location.port ? ":" + window.location.port : "";
// let host = "http://" + window.location.host + port;


// console.log(host);
let url = "/resourceLevel/getMallParameter";
let params = [
    "mall_goods_host",
    "mall_purchase_host",
    "mall_file_host",
    "mall_base_host",
    "mall_contract_host",
    "mall_finance_host",
    "mall_reportform_host",
    "mall_sale_host",
    "mall_supplier_host",
    "mall_wholesale_host",
    "mall_storage_host",
    "njxs_mall_erp_enterpriseid",
    // "se_mall_enterpriseid"
]
let options = {
    method: "post",
    data: {"parameterStr": params},
    url: host + "/resourceLevel/getMallParameter",
	timeout: 10000
}
// console.log("这是参数", options.data);

export default function (callback, errFn) {
    axios(options).then(data => {
        if (data.status === 200 && data.data.data[0]) {
            console.log(data.data.data[0]);
            initAPI(data.data.data[0]);
            if (callback) {
                callback();
            }
        }
    }).catch(err => {
    	console.log(err);
        initCatchAPI();
        console.log("缓存的地址");
        if (errFn) {
            errFn();
        }
    });
}

function initAPI(res) {
    let data = {};
    for (var i = 0; i < res.length; i++) {
        // if (res[i].key === "se_mall_enterpriseid") {
        if (res[i].key === "njxs_mall_erp_enterpriseid") {
            data[res[i].key] = res[i].value;
        } else {
            data[res[i].key] = res[i].value.slice(0, res[i].value.length - 1);
            //如果是测试环境就作处理，去掉最后的"/"
            // if (res[i].key === "mall_file_host" && res[i].value.lastIndexOf("/") === res[i].value.length - 1) {
            //     data[res[i].key] = res[i].value.slice(0, res[i].value.length - 1);

            // } else {

            // }
        }
    }
    if (data.mall_file_host === "http://files.scn.weilian.c") {
        data.mall_file_host = data.mall_file_host + "n";
    }
    window.api = {
        ...data,
        local_host: host,
        FILE_HOST: data.mall_file_host,
        // mall_file_host: data.mall_file_host + "n",
        "enterpriseId": data.njxs_mall_erp_enterpriseid,
        "enterpriseid": data.njxs_mall_erp_enterpriseid,
        "enterpriseCode": 'NJXS',
        // "enterpriseId": data.se_mall_enterpriseid,
        // "enterpriseid": data.se_mall_enterpriseid
    }
    //判断生产环境还是测试环境

}

function initCatchAPI() {
    window.api = {
		// "local_host": "http://test.mall.yn.weilian.cn",//运营后台地址
		"local_host": host,//运营后台地址
		// "local_host": "http://test.njxs.weilian.cn:8112",//运营后台地址
		// "local_host": "http://172.19.5.145:8112",//运营后台地址
		"mall_base_host":	    "http://test.base.scn.weilian.cn",	//商城调用base模块的地址
		"mall_contract_host":	"http://test.contract.scn.weilian.cn",	 //商城调用contract模块的地址
		"mall_finance_host":	"http://test.finance.scn.weilian.cn",	 //商城调用finance模块的地址
		"mall_purchase_host":	"http://test.purchase.scn.weilian.cn",	 //商城调用purchase模块的地址
		"mall_reportform_host":	"http://test.reportform.scn.weilian.cn",	 //商城调用reportform模块的地址
		"mall_sale_host":	     "http://test.sale.scn.weilian.cn",	 //商城调用sale模块的地址
		"mall_supplier_host":	"http://test.supplier.scn.weilian.cn",	  //商城调用supplier模块的地址
		"mall_wholesale_host":	"http://test.wholesale.scn.weilian.cn",	//商城调用wholesale模块的地址
		"mall_storage_host":	"http://test.storage.scn.weilian.cn",	//商城调用storage模块的地址,
		"mall_goods_host": "http://test.goods.scn.weilian.cn", //商城调用goods模块的地址
		"mall_file_host": "http://files.scn.weilian.cn", //商城调用文件服务器地址
		"enterpriseId": 134,
		"enterpriseid": 134,
        "enterpriseCode": 'NJXS',
	}
}

